#include "main.h"

GLuint Enemy::attacksprites[5][2];
GLuint Enemy::tracersprites[5][2];
GLuint Enemy::guardsprites[5][2];
GLuint Enemy::fearsprites[5][2];
GLuint Enemy::racersprites[5][2];
CSound *Enemy::sounds[5];
////////////////////////////////////////////////////////
//Enemy Parent Class
////////////////////////////////////////////////////////
void Enemy::loadsprites()
{
	char *attackfiles[] =
	{ 
		"sprite/attack.bmp",
		"sprite/attack2.bmp",
		"sprite/attack3.bmp",
		"sprite/attack4.bmp",
		"sprite/attack5.bmp",
		"sprite/attack6.bmp",
		"sprite/attack7.bmp",
		"sprite/attack8.bmp",
		"sprite/attackd.bmp",
		"sprite/attackd2.bmp"
	};	
	
	char *tracerfiles[] =
	{
		"sprite/tracer.bmp",
		"sprite/tracer2.bmp",
		"sprite/tracer3.bmp",
		"sprite/tracer4.bmp",
		"sprite/tracer5.bmp",
		"sprite/tracer6.bmp",
		"sprite/tracer7.bmp",
		"sprite/tracer8.bmp",
		"sprite/enemyd.bmp",
		"sprite/enemyd2.bmp"
	};

	char *guardfiles[] =
	{
		"sprite/guard.bmp",
		"sprite/guard2.bmp",
		"sprite/guard3.bmp",
		"sprite/guard4.bmp",
		"sprite/guard5.bmp",
		"sprite/guard6.bmp",
		"sprite/guard7.bmp",
		"sprite/guard8.bmp",
		"sprite/enemyd.bmp",
		"sprite/enemyd2.bmp"
	};

	char *fearfiles[] =
	{
		"sprite/fear.bmp",
		"sprite/fear2.bmp",
		"sprite/fear3.bmp",
		"sprite/fear4.bmp",
		"sprite/fear5.bmp",
		"sprite/fear6.bmp",
		"sprite/fear7.bmp",
		"sprite/fear8.bmp",
		"sprite/enemyd.bmp",
		"sprite/enemyd2.bmp"
	};

	char *racerfiles[] =
	{
		"sprite/racer.bmp",
		"sprite/racer2.bmp",
		"sprite/racer3.bmp",
		"sprite/racer4.bmp",
		"sprite/racer5.bmp",
		"sprite/racer6.bmp",
		"sprite/racer7.bmp",
		"sprite/racer8.bmp",
		"sprite/racerd.bmp",
		"sprite/racerd2.bmp"
	};

	
	
	int count = 0;
	int index;
	for(index = 0; index < 5; index++)
	{
		for(int loop = 0; loop < 2; loop++)
		{
			attacksprites[index][loop] = LoadGLTextures(attackfiles[count]);
			count++;
		}
	}

	count = 0;
	
	for(index = 0; index < 5; index++)
	{
		for(int loop = 0; loop < 2; loop++)
		{
			tracersprites[index][loop] = LoadGLTextures(tracerfiles[count]);
			count++;
		}
	}

	count = 0;
	for(index = 0; index < 5; index++)
	{
		for(int loop = 0; loop < 2; loop++)
		{
			guardsprites[index][loop] = LoadGLTextures(guardfiles[count]);
			count++;
		}
	}

	count = 0;
	for(index = 0; index < 5; index++)
	{
		for(int loop = 0; loop < 2; loop++)
		{
			fearsprites[index][loop] = LoadGLTextures(fearfiles[count]);
			count++;
		}
	}

	count = 0;
	for(index = 0; index < 5; index++)
	{
		for(int loop = 0; loop < 2; loop++)
		{
			racersprites[index][loop] = LoadGLTextures(racerfiles[count]);
			count++;
		}
	}

	char *enemysounds[] = 
	{
		"sound/enemydeath.wav",
		"sound/fear.wav",
		"sound/guard.wav",
		"sound/tracer.wav",
		"sound/racer.wav"
	};

	for(int z = 0; z < 5; z++)
	{
		sounds[z] = soundsystem.LoadSound(enemysounds[z]);
	}

}

/***********************************************************************
Determines if Pachead touches an enemy
***********************************************************************/
void Enemy::collision()
{
	if(  ((x+17 >= pachead.x && x+17 <= pachead.x+17) &&
		 (y+17 >= pachead.y && y+17 <= pachead.y+17))
		 ||
			((x+7 >= pachead.x && x+7 <= pachead.x+17) &&
			(y+7 >= pachead.y && y+7 <= pachead.y+17))  )
	{

		if(hit == false)
		{
			if(pachead.state == POWER && direction != DEAD)
			{
				direction = DEAD;
				timer = timeGetTime();
				pachead.score += 100;
				soundsystem.PlaySound(sounds[id]);
			}
			else
			{

				if(pachead.state == INVIS)
				{
					if(pachead.moving && direction != DEAD)
					{
						hit = true;
						pachead.health -= (34*difficulty);
						if(pachead.health > 0)
						{
							if(difficulty == 1)
								soundsystem.PlaySound(pachead.sounds[PACHEADHIT]);
							else
								soundsystem.PlaySound(pachead.dygersound[5]);
						}

					}

					
				}
				else
				{
					if(direction != DEAD)
					{
						hit = true;
						pachead.health -= (34*difficulty);
						if(pachead.health > 0)
						{
							if(difficulty == 1)
								soundsystem.PlaySound(pachead.sounds[PACHEADHIT]);
							else
								soundsystem.PlaySound(pachead.dygersound[5]);
						}
					}
				}
			}
		}
		
	}
	else
	{
		hit = false;
	}

	if(pachead.health <= 0)
		pachead.death();

}

void Enemy::ai()
{

}

void Enemy::drawenemy()
{

}


//////////////////////////////////////////////////////////////////////
//ATTACKER CODE
///////////////////////////////////////////////////////////////////////
void Attack::ai()
{
	static bool directions[4];
	directions[0] = false;
	directions[1] = false;
	directions[2] = false;
	directions[3] = false;
	
	if( (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0 )
	{
		if( 
			(direction == RIGHT || direction == LEFT) && 
			((map.tiles[posy-1][posx] != WALL && map.tiles[posy-1][posx] != SWALL) ||
			 (map.tiles[posy+1][posx] != WALL && map.tiles[posy+1][posx] != SWALL))   
			||
			(direction == UP || direction == DOWN) && 
			((map.tiles[posy][posx-1] != WALL && map.tiles[posy][posx-1] != SWALL) ||
			(map.tiles[posy][posx+1] != WALL && map.tiles[posy][posx+1] != SWALL))
		  )
		{
			if(map.tiles[posy][posx+1] != WALL && map.tiles[posy][posx+1] != SWALL)
				directions[RIGHT] = true;
			if(map.tiles[posy][posx-1] != WALL && map.tiles[posy][posx-1] != SWALL)
				directions[LEFT] = true;
			if(map.tiles[posy-1][posx] != WALL && map.tiles[posy-1][posx] != SWALL)
				directions[UP] = true;
			if(map.tiles[posy+1][posx] != WALL && map.tiles[posy+1][posx] != SWALL)
				directions[DOWN] = true;

			//////////////////////////////////////
			//prioritize the available directions
			//////////////////////////////////////
			if(pachead.state != INVIS)
			{

				if(directions[RIGHT] == true)
				{
					if(x < pachead.x)
					{
						directions[LEFT] = false;
						if(abs(x-pachead.x) > abs(y-pachead.y))
						{
							directions[UP] = false;
							directions[DOWN] = false;
						}
					}
				}
				//left
				if(directions[LEFT] == true)
				{
					if(x > pachead.x)
					{
						directions[RIGHT] = false;
						if((GLfloat)abs(x-pachead.x) > (GLfloat)abs(y-pachead.y))
						{
							directions[UP] = false;
							directions[DOWN] = false;
						}
					}
				}
				//up
				if(directions[UP] == true)
				{
					if(y < pachead.y)
					{
						directions[DOWN] = false;
						if(abs(x-pachead.x) < abs(y-pachead.y))
						{
							directions[RIGHT] = false;
							directions[LEFT] = false;
						}
					}
				}
				//down
				if(directions[DOWN] == true)
				{
					if(y > pachead.y)
					{
						directions[UP] = false;
						if(abs(x-pachead.x) < abs(y-pachead.y))
						{
							directions[RIGHT] = false;
							directions[LEFT] = false;
						}
					}
				}

			}

			do
			{
				direction = rand()%4;
			}while(!directions[direction]);

		}

		if(direction == RIGHT && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && map.tiles[posy][posx-1] != WALL && 
		   map.tiles[posy][posx-1] != SWALL)
			direction = LEFT;

		if(direction == LEFT && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && map.tiles[posy][posx+1] != WALL && 
		   map.tiles[posy][posx+1] != SWALL)
			direction = RIGHT;

		if(direction == UP && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && map.tiles[posy+1][posx] != WALL && 
		   map.tiles[posy+1][posx] != SWALL)
			direction = DOWN;

		if(direction == DOWN && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && map.tiles[posy-1][posx] != WALL && 
		   map.tiles[posy-1][posx] != SWALL)
			direction = UP;



	}


	////////////////
	//move Attacker
	///////////////
	switch(direction)
	{
	case RIGHT:
		{
			x+=speed;
			if(x > 504*scale-((float)W_WIDTH/2))//this code wraps pachead around the screen
			{
				if(state != SPEED)
					x = 2*scale - ((float)W_WIDTH/2);
				else
					x = 4*scale - ((float)W_WIDTH/2);
			}
		}break;
	case LEFT: 
		{
			x-=speed;
			if(x < 0-((float)W_WIDTH/2))
			{
				if(state != SPEED)
					x = 502*scale-((float)W_WIDTH/2);
				else
					x = 500*scale-((float)W_WIDTH/2);
			}

			
		}break;
	case DOWN:
		{
			y-=speed;
			if( y < (W_HEIGHT-(SIZEY*B_SIZE))-((float)W_HEIGHT/2))
				y = (GLfloat)W_HEIGHT-B_SIZE-((float)W_HEIGHT/2);
		}break;
	case UP: 
		{
			y+=speed;
			if( y > W_HEIGHT-B_SIZE-((float)W_HEIGHT/2))
				y = (GLfloat)W_HEIGHT-(SIZEY*B_SIZE)-((float)W_HEIGHT/2);

		}break;
	};
	
	//////////////////////
	//hold back if a wall
	//////////////////////
	if(direction == RIGHT && (map.tiles[posy][posx+1] == WALL || map.tiles[posy][posx+1] == SWALL))  
		x-=speed;
	if(direction == LEFT && (map.tiles[posy][posx-1] == WALL || map.tiles[posy][posx-1] == SWALL))
		x+=speed;
	if(direction == DOWN && (map.tiles[posy+1][posx] == WALL || map.tiles[posy+1][posx] == SWALL))
		y+=speed;
	if(direction == UP && (map.tiles[posy-1][posx] == WALL || map.tiles[posy-1][posx] == SWALL))
		y-=speed;	

  
		
	if(direction == DEAD)
	{
		if((int)x < map.x[initposy][initposx])
			x+=1;
		if((int)y < map.y[initposy][initposx])
			y+=1;
		if((int)x > map.x[initposy][initposx])
			x-=1;
		if((int)y > map.y[initposy][initposx])
			y-=1;

		if((int)x == map.x[initposy][initposx] && 
			(int)y == map.y[initposy][initposx] && 
			timeGetTime()-timer >= GHOSTTIME)
		{
			direction = RIGHT;
			posx = initposx;
			posy = initposy;
			
			x = map.x[initposy][initposx];
			y = map.y[initposy][initposx];
		}
	}
	/////////////////////////
	//update tile position
	/////////////////////////
	if( (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0 )
	{
		posx = (int)(x+((float)W_WIDTH/2))/B_SIZE;
		posy = 19-(int)(y+((float)W_HEIGHT/2))/B_SIZE;
	}
}

void Attack::drawenemy()
{
	if(direction == DEAD)
	{
		glBindTexture(GL_TEXTURE_2D, attacksprites[DEAD][FRAME]);
	}
	else
	{
		glBindTexture(GL_TEXTURE_2D, attacksprites[DOWN][FRAME]);
	}

	//glColor3f(ENEMYBLUE);
	/*
	glBegin(GL_QUADS); //draw the tiles
		glTexCoord2f(0.0f, 0.0f); glVertex3f((GLfloat)x, (GLfloat)y,  5.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f( (GLfloat)x+B_SIZE, (GLfloat)y,  5.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( (GLfloat)x+B_SIZE,  (GLfloat)y+B_SIZE,  5.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f((GLfloat)x,  (GLfloat)y+B_SIZE,  5.0f);	// Top Left Of The Texture and Quad
	glEnd();
	*/

	GLUquadric * sphere = gluNewQuadric();
	gluQuadricTexture(sphere, true);

	glPushMatrix();
	glTranslated(x + B_SIZE/2, y + B_SIZE/2,  5.0f);

	switch(direction){
		case LEFT:
			{
				glRotated(-90,0,0,1);
				break;
			}
		case DOWN:
			{
				//glRotated(0,0,0,1);
				break;
			}
		case RIGHT:
			{
				glRotated(90,0,0,1);
				break;
			}
		case UP:
			{
				glRotated(180,0,0,1);
				break;
			}
	};

	gluSphere(sphere, B_SIZE/3, 20, 20);
	glPopMatrix();
			
	gluDeleteQuadric(sphere);

	glColor3f(WHITE);


}

//////////////////////////////////////////////////////////////////////
//FEAR CODE
///////////////////////////////////////////////////////////////////////
void Fear::ai()
{
	static bool directions[4];
	directions[0] = false;
	directions[1] = false;
	directions[2] = false;
	directions[3] = false;
	
	if( (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0 )
	{
		if( 
			(direction == RIGHT || direction == LEFT) && 
			((map.tiles[posy-1][posx] != WALL && map.tiles[posy-1][posx] != SWALL) ||
			 (map.tiles[posy+1][posx] != WALL && map.tiles[posy+1][posx] != SWALL))   
			||
			(direction == UP || direction == DOWN) && 
			((map.tiles[posy][posx-1] != WALL && map.tiles[posy][posx-1] != SWALL) ||
			(map.tiles[posy][posx+1] != WALL && map.tiles[posy][posx+1] != SWALL))
		  )
		{
			if(map.tiles[posy][posx+1] != WALL && map.tiles[posy][posx+1] != SWALL)
				directions[RIGHT] = true;
			if(map.tiles[posy][posx-1] != WALL && map.tiles[posy][posx-1] != SWALL)
				directions[LEFT] = true;
			if(map.tiles[posy-1][posx] != WALL && map.tiles[posy-1][posx] != SWALL)
				directions[UP] = true;
			if(map.tiles[posy+1][posx] != WALL && map.tiles[posy+1][posx] != SWALL)
				directions[DOWN] = true;

			//////////////////////////////////////
			//prioritize the available directions
			//////////////////////////////////////

			if(pachead.state != INVIS)
			{
				if(directions[RIGHT] == true)
				{
					if(x > pachead.x)
					{
						directions[LEFT] = false;
						if(abs(x-pachead.x) > abs(y-pachead.y))
						{
							directions[UP] = false;
							directions[DOWN] = false;
						}
					}
				}
				//left
				if(directions[LEFT] == true)
				{
					if(x < pachead.x)
					{
						directions[RIGHT] = false;
						if(abs(x-pachead.x) > abs(y-pachead.y))
						{
							directions[UP] = false;
							directions[DOWN] = false;
						}
					}
				}
				//up
				if(directions[UP] == true)
				{
					if(y > pachead.y)
					{
						directions[DOWN] = false;
						if(abs(x-pachead.x) < abs(y-pachead.y))
						{
							directions[RIGHT] = false;
							directions[LEFT] = false;
						}
					}
				}
				//dow
				if(directions[DOWN] == true)
				{
					if(y < pachead.y)
					{
						directions[UP] = false;
						if(abs(x-pachead.x) < abs(y-pachead.y))
						{
							directions[RIGHT] = false;
							directions[LEFT] = false;
						}
					}
				}
			}

			do
			{
				direction = rand()%4;
			}while(!directions[direction]);

		}

		if(direction == RIGHT && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && map.tiles[posy][posx-1] != WALL && 
		   map.tiles[posy][posx-1] != SWALL)
			direction = LEFT;

		if(direction == LEFT && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && map.tiles[posy][posx+1] != WALL && 
		   map.tiles[posy][posx+1] != SWALL)
			direction = RIGHT;

		if(direction == UP && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && map.tiles[posy+1][posx] != WALL && 
		   map.tiles[posy+1][posx] != SWALL)
			direction = DOWN;

		if(direction == DOWN && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && map.tiles[posy-1][posx] != WALL && 
		   map.tiles[posy-1][posx] != SWALL)
			direction = UP;



	}

	////////////////
	//move Attacker
	///////////////
	switch(direction)
	{
	case RIGHT:
		{
			x+=speed;
			if(x > 504*scale-((float)W_WIDTH/2))//this code wraps pachead around the screen
			{
				if(state != SPEED)
					x = 2*scale - ((float)W_WIDTH/2);
				else
					x = 4*scale - ((float)W_WIDTH/2);
			}
		}break;
	case LEFT: 
		{
			x-=speed;
			if(x < 0-((float)W_WIDTH/2))
			{
				if(state != SPEED)
					x = 502*scale-((float)W_WIDTH/2);
				else
					x = 500*scale-((float)W_WIDTH/2);
			}

			
		}break;
	case DOWN:
		{
			y-=speed;
			if( y < (W_HEIGHT-(SIZEY*B_SIZE))-((float)W_HEIGHT/2))
				y = (GLfloat)W_HEIGHT-B_SIZE-((float)W_HEIGHT/2);
		}break;
	case UP: 
		{
			y+=speed;
			if( y > W_HEIGHT-B_SIZE-((float)W_HEIGHT/2))
				y = (GLfloat)W_HEIGHT-(SIZEY*B_SIZE)-((float)W_HEIGHT/2);

		}break;
	};
	
	//////////////////////
	//hold back if a wall
	//////////////////////
	if(direction == RIGHT && (map.tiles[posy][posx+1] == WALL || map.tiles[posy][posx+1] == SWALL))  
		x-=speed;
	if(direction == LEFT && (map.tiles[posy][posx-1] == WALL || map.tiles[posy][posx-1] == SWALL))
		x+=speed;
	if(direction == DOWN && (map.tiles[posy+1][posx] == WALL || map.tiles[posy+1][posx] == SWALL))
		y+=speed;
	if(direction == UP && (map.tiles[posy-1][posx] == WALL || map.tiles[posy-1][posx] == SWALL))
		y-=speed;	

  
		
	if(direction == DEAD)
	{
		if((int)x < map.x[initposy][initposx])
			x+=1;
		if((int)y < map.y[initposy][initposx])
			y+=1;
		if((int)x > map.x[initposy][initposx])
			x-=1;
		if((int)y > map.y[initposy][initposx])
			y-=1;

		if((int)x == map.x[initposy][initposx] && 
			(int)y == map.y[initposy][initposx] && 
			timeGetTime()-timer >= GHOSTTIME)
		{
			direction = RIGHT;
			posx = initposx;
			posy = initposy;
			
			x = map.x[initposy][initposx];
			y = map.y[initposy][initposx];
		}
	}
	/////////////////////////
	//update tile position
	/////////////////////////
	if( (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0 )
	{
		posx = (int)(x+((float)W_WIDTH/2))/B_SIZE;
		posy = 19-(int)(y+((float)W_HEIGHT/2))/B_SIZE;
	}
}

void Fear::drawenemy()
{
	if(direction == DEAD)
	{
		glBindTexture(GL_TEXTURE_2D, fearsprites[DEAD][FRAME]);
	}
	else
	{
		glBindTexture(GL_TEXTURE_2D, fearsprites[DOWN][FRAME]);
	}
	
	if(direction == DEAD)
		glColor3f(ENEMYPURP);
	
	/*
	glBegin(GL_QUADS); //draw the tiles
		glTexCoord2f(0.0f, 0.0f); glVertex3f((GLfloat)x, (GLfloat)y,  5.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f( (GLfloat)x+B_SIZE, (GLfloat)y,  5.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( (GLfloat)x+B_SIZE,  (GLfloat)y+B_SIZE,  5.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f((GLfloat)x,  (GLfloat)y+B_SIZE,  5.0f);	// Top Left Of The Texture and Quad
	glEnd();
	*/

	GLUquadric * sphere = gluNewQuadric();
	gluQuadricTexture(sphere, true);

	glPushMatrix();
	glTranslated(x + B_SIZE/2, y + B_SIZE/2,  5.0f);

	switch(direction){
		case LEFT:
			{
				glRotated(-90,0,0,1);
				break;
			}
		case DOWN:
			{
				//glRotated(0,0,0,1);
				break;
			}
		case RIGHT:
			{
				glRotated(90,0,0,1);
				break;
			}
		case UP:
			{
				glRotated(180,0,0,1);
				break;
			}
	};
	gluSphere(sphere, B_SIZE/3, 20, 20);
	glPopMatrix();
			
	gluDeleteQuadric(sphere);

	glColor3f(WHITE);

}

//////////////////////////////////////////////////////////////////////
//GUARD CODE
///////////////////////////////////////////////////////////////////////
void Guard::ai()
{
	
	static bool directions[4];
	directions[0] = false;
	directions[1] = false;
	directions[2] = false;
	directions[3] = false;

	/////////////////////////
	//check for intersection
	/////////////////////////
	if( (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0 )
	{
		if( 
			(direction == RIGHT || direction == LEFT) && 
			((map.tiles[posy-1][posx] != WALL && map.tiles[posy-1][posx] != SWALL) ||
			 (map.tiles[posy+1][posx] != WALL && map.tiles[posy+1][posx] != SWALL))   
			||
			(direction == UP || direction == DOWN) && 
			((map.tiles[posy][posx-1] != WALL && map.tiles[posy][posx-1] != SWALL) ||
			(map.tiles[posy][posx+1] != WALL && map.tiles[posy][posx+1] != SWALL))
		  )
		{   ////////////////////////////////
			//note the available directions
			////////////////////////////////
			if(map.tiles[posy][posx+1] != WALL && map.tiles[posy][posx+1] != SWALL)
				directions[RIGHT] = true;
			if(map.tiles[posy][posx-1] != WALL && map.tiles[posy][posx-1] != SWALL)
				directions[LEFT] = true;
			if(map.tiles[posy-1][posx] != WALL && map.tiles[posy-1][posx] != SWALL)
				directions[UP] = true;
			if(map.tiles[posy+1][posx] != WALL && map.tiles[posy+1][posx] != SWALL)
				directions[DOWN] = true;
			
			if(abs(initposx-pachead.posx) < 4 && abs(initposy-pachead.posy) < 4)
			{
			//////////////////////////////////////
			//prioritize the available directions
			//////////////////////////////////////

			if(pachead.state != INVIS)
			{
				if(directions[RIGHT] == true)
				{
					if(x < pachead.x)
					{
						directions[LEFT] = false;
						if(abs(x-pachead.x) > abs(y-pachead.y))
						{
							directions[UP] = false;
							directions[DOWN] = false;
						}
					}
				}
				//left
				if(directions[LEFT] == true)
				{
					if(x > pachead.x)
					{
						directions[RIGHT] = false;
						if((GLfloat)abs(x-pachead.x) > (GLfloat)abs(y-pachead.y))
						{
							directions[UP] = false;
							directions[DOWN] = false;
						}
					}
				}
				//up
				if(directions[UP] == true)
				{
					if(y < pachead.y)
					{	
						directions[DOWN] = false;
						if(abs(x-pachead.x) < abs(y-pachead.y))
						{
							directions[RIGHT] = false;
							directions[LEFT] = false;
						}
					}
				}
				//down
				if(directions[DOWN] == true)
				{
					if(y > pachead.y)
					{
						directions[UP] = false;
						if(abs(x-pachead.x) < abs(y-pachead.y))
						{
							directions[RIGHT] = false;
							directions[LEFT] = false;
						}
					}
				}
			}

			}
			
			do//randomly pick from remaining directions
			{
				direction = (rand()%4);
			}while(!directions[direction]);
			
		}

		if(posx-initposx > 4)
			direction = LEFT;
		if(initposx-posx > 4)
			direction = RIGHT;
		if(posy-initposy > 4)
			direction = UP;
		if(initposy-posy > 4)
			direction = DOWN;


		if(direction == RIGHT && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && map.tiles[posy][posx-1] != WALL && 
		   map.tiles[posy][posx-1] != SWALL)
			direction = LEFT;

		if(direction == LEFT && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && map.tiles[posy][posx+1] != WALL && 
		   map.tiles[posy][posx+1] != SWALL)
			direction = RIGHT;

		if(direction == UP && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && map.tiles[posy+1][posx] != WALL && 
		   map.tiles[posy+1][posx] != SWALL)
			direction = DOWN;

		if(direction == DOWN && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && map.tiles[posy-1][posx] != WALL && 
		   map.tiles[posy-1][posx] != SWALL)
			direction = UP;
				
	}


	////////////////
	//move Attacker
	///////////////
	switch(direction)
	{
	case RIGHT:
		{
			x+=speed;
			if(x > 504*scale-((float)W_WIDTH/2))//this code wraps pachead around the screen
			{
				if(state != SPEED)
					x = 2*scale - ((float)W_WIDTH/2);
				else
					x = 4*scale - ((float)W_WIDTH/2);
			}
		}break;
	case LEFT: 
		{
			x-=speed;
			if(x < 0-((float)W_WIDTH/2))
			{
				if(state != SPEED)
					x = 502*scale-((float)W_WIDTH/2);
				else
					x = 500*scale-((float)W_WIDTH/2);
			}

			
		}break;
	case DOWN:
		{
			y-=speed;
			if( y < (W_HEIGHT-(SIZEY*B_SIZE))-((float)W_HEIGHT/2))
				y = (GLfloat)W_HEIGHT-B_SIZE-((float)W_HEIGHT/2);
		}break;
	case UP: 
		{
			y+=speed;
			if( y > W_HEIGHT-B_SIZE-((float)W_HEIGHT/2))
				y = (GLfloat)W_HEIGHT-(SIZEY*B_SIZE)-((float)W_HEIGHT/2);

		}break;
	};
	
	//////////////////////
	//hold back if a wall
	//////////////////////
	if(direction == RIGHT && (map.tiles[posy][posx+1] == WALL || map.tiles[posy][posx+1] == SWALL))  
		x-=speed;
	if(direction == LEFT && (map.tiles[posy][posx-1] == WALL || map.tiles[posy][posx-1] == SWALL))
		x+=speed;
	if(direction == DOWN && (map.tiles[posy+1][posx] == WALL || map.tiles[posy+1][posx] == SWALL))
		y+=speed;
	if(direction == UP && (map.tiles[posy-1][posx] == WALL || map.tiles[posy-1][posx] == SWALL))
		y-=speed;	

  
		
	if(direction == DEAD)
	{
		if((int)x < map.x[initposy][initposx])
			x+=1;
		if((int)y < map.y[initposy][initposx])
			y+=1;
		if((int)x > map.x[initposy][initposx])
			x-=1;
		if((int)y > map.y[initposy][initposx])
			y-=1;

		if((int)x == map.x[initposy][initposx] && 
			(int)y == map.y[initposy][initposx] && 
			timeGetTime()-timer >= GHOSTTIME)
		{
			direction = RIGHT;
			posx = initposx;
			posy = initposy;
			
			x = map.x[initposy][initposx];
			y = map.y[initposy][initposx];
		}
	}
	/////////////////////////
	//update tile position
	/////////////////////////
	if( (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0 )
	{
		posx = (int)(x+((float)W_WIDTH/2))/B_SIZE;
		posy = 19-(int)(y+((float)W_HEIGHT/2))/B_SIZE;
	}
}

void Guard::drawenemy()
{
	if(direction == DEAD)
	{
		glBindTexture(GL_TEXTURE_2D, guardsprites[DEAD][FRAME]);
	}
	else
	{
		glBindTexture(GL_TEXTURE_2D, guardsprites[DOWN][FRAME]);
	}

	glColor3f(ENEMYGRAY);
	
	/*
	glBegin(GL_QUADS); //draw the tiles
		glTexCoord2f(0.0f, 0.0f); glVertex3f((GLfloat)x, (GLfloat)y,  5.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f( (GLfloat)x+B_SIZE, (GLfloat)y,  5.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( (GLfloat)x+B_SIZE,  (GLfloat)y+B_SIZE,  5.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f((GLfloat)x,  (GLfloat)y+B_SIZE,  5.0f);	// Top Left Of The Texture and Quad
	glEnd();
	*/

	GLUquadric * sphere = gluNewQuadric();
	gluQuadricTexture(sphere, true);

	glPushMatrix();
	glTranslated(x + B_SIZE/2, y + B_SIZE/2,  5.0f);

	switch(direction){
		case LEFT:
			{
				glRotated(-90,0,0,1);
				break;
			}
		case DOWN:
			{
				//glRotated(0,0,0,1);
				break;
			}
		case RIGHT:
			{
				glRotated(90,0,0,1);
				break;
			}
		case UP:
			{
				glRotated(180,0,0,1);
				break;
			}
	};

	gluSphere(sphere, B_SIZE/3, 20, 20);
	glPopMatrix();
			
	gluDeleteQuadric(sphere);

	glColor3f(WHITE);
}

//////////////////////////////////////////////////////////////////////
//TRACER CODE
///////////////////////////////////////////////////////////////////////
void Tracer::ai()
{
	if( (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0 )
	{
		if( 
			(direction == RIGHT || direction == LEFT) && 
			((map.tiles[posy-1][posx] != WALL && map.tiles[posy-1][posx] != SWALL) ||
			 (map.tiles[posy+1][posx] != WALL && map.tiles[posy+1][posx] != SWALL))   
			||
			(direction == UP || direction == DOWN) && 
			((map.tiles[posy][posx-1] != WALL && map.tiles[posy][posx-1] != SWALL) ||
			(map.tiles[posy][posx+1] != WALL && map.tiles[posy][posx+1] != SWALL))
		  )
		{
			switch(direction)
			{
			case DOWN:
			{
				if(map.tiles[posy][posx-1] != WALL && map.tiles[posy][posx-1] != SWALL)
					direction = LEFT;
				else
					if(map.tiles[posy+1][posx] != WALL && map.tiles[posy+1][posx] != SWALL)
						direction = DOWN;
					else
						if(map.tiles[posy][posx+1] != WALL && map.tiles[posy][posx+1] != SWALL)
							direction = RIGHT;
			}break;
		
			case UP:
			{
				if(map.tiles[posy][posx+1] != WALL && map.tiles[posy][posx+1] != SWALL)
					direction = RIGHT;
				else
					if(map.tiles[posy-1][posx] != WALL && map.tiles[posy-1][posx] != SWALL)
						direction = UP;
					else
						if(map.tiles[posy][posx-1] != WALL && map.tiles[posy][posx-1] != SWALL)
							direction = LEFT;
			}break;

			case RIGHT:
			{
				if(map.tiles[posy+1][posx] != WALL && map.tiles[posy+1][posx] != SWALL)
					direction = DOWN;
				else
					if(map.tiles[posy][posx+1] != WALL && map.tiles[posy][posx+1] != SWALL)
						direction = RIGHT;
					else
						if(map.tiles[posy-1][posx] != WALL && map.tiles[posy-1][posx] != SWALL)
							direction = UP;
			}break;

			case LEFT:
			{
				if(map.tiles[posy-1][posx] != WALL && map.tiles[posy-1][posx] != SWALL)
					direction = UP;
				else
					if(map.tiles[posy][posx-1] != WALL && map.tiles[posy][posx-1] != SWALL)
						direction = LEFT;
					else
						if(map.tiles[posy+1][posx] != WALL && map.tiles[posy+1][posx] != SWALL)
							direction = DOWN;
			}break;
			};

			
			
		}

		if(direction == RIGHT && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && map.tiles[posy][posx-1] != WALL && 
		   map.tiles[posy][posx-1] != SWALL)
			direction = LEFT;

		if(direction == LEFT && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && map.tiles[posy][posx+1] != WALL && 
		   map.tiles[posy][posx+1] != SWALL)
			direction = RIGHT;

		if(direction == UP && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && map.tiles[posy+1][posx] != WALL && 
		   map.tiles[posy+1][posx] != SWALL)
			direction = DOWN;

		if(direction == DOWN && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && map.tiles[posy-1][posx] != WALL && 
		   map.tiles[posy-1][posx] != SWALL)
			direction = UP;

	}

	////////////////
	//move Attacker
	///////////////
	switch(direction)
	{
	case RIGHT:
		{
			x+=speed;
			if(x > 504*scale-((float)W_WIDTH/2))//this code wraps pachead around the screen
			{
				if(state != SPEED)
					x = 2*scale - ((float)W_WIDTH/2);
				else
					x = 4*scale - ((float)W_WIDTH/2);
			}
		}break;
	case LEFT: 
		{
			x-=speed;
			if(x < 0-((float)W_WIDTH/2))
			{
				if(state != SPEED)
					x = 502*scale-((float)W_WIDTH/2);
				else
					x = 500*scale-((float)W_WIDTH/2);
			}

			
		}break;
	case DOWN:
		{
			y-=speed;
			if( y < (W_HEIGHT-(SIZEY*B_SIZE))-((float)W_HEIGHT/2))
				y = (GLfloat)W_HEIGHT-B_SIZE-((float)W_HEIGHT/2);
		}break;
	case UP: 
		{
			y+=speed;
			if( y > W_HEIGHT-B_SIZE-((float)W_HEIGHT/2))
				y = (GLfloat)W_HEIGHT-(SIZEY*B_SIZE)-((float)W_HEIGHT/2);

		}break;
	};
	
	//////////////////////
	//hold back if a wall
	//////////////////////
	if(direction == RIGHT && (map.tiles[posy][posx+1] == WALL || map.tiles[posy][posx+1] == SWALL))  
		x-=speed;
	if(direction == LEFT && (map.tiles[posy][posx-1] == WALL || map.tiles[posy][posx-1] == SWALL))
		x+=speed;
	if(direction == DOWN && (map.tiles[posy+1][posx] == WALL || map.tiles[posy+1][posx] == SWALL))
		y+=speed;
	if(direction == UP && (map.tiles[posy-1][posx] == WALL || map.tiles[posy-1][posx] == SWALL))
		y-=speed;	

  
		
	if(direction == DEAD)
	{
		if((int)x < map.x[initposy][initposx])
			x+=1;
		if((int)y < map.y[initposy][initposx])
			y+=1;
		if((int)x > map.x[initposy][initposx])
			x-=1;
		if((int)y > map.y[initposy][initposx])
			y-=1;

		if((int)x == map.x[initposy][initposx] && 
			(int)y == map.y[initposy][initposx] && 
			timeGetTime()-timer >= GHOSTTIME)
		{
			direction = RIGHT;
			posx = initposx;
			posy = initposy;
			
			x = map.x[initposy][initposx];
			y = map.y[initposy][initposx];
		}
	}
	/////////////////////////
	//update tile position
	/////////////////////////
	if( (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0 )
	{
		posx = (int)(x+((float)W_WIDTH/2))/B_SIZE;
		posy = 19-(int)(y+((float)W_HEIGHT/2))/B_SIZE;
	}
}

void Tracer::drawenemy()
{

	
	if(direction == DEAD)
	{
		glBindTexture(GL_TEXTURE_2D, tracersprites[DEAD][FRAME]);
	}
	else
	{
		glBindTexture(GL_TEXTURE_2D, tracersprites[DOWN][FRAME]);
	}


	if(direction == DEAD)
		glColor3f(ENEMYTEAL);
	
	/*
	glBegin(GL_QUADS); //draw the tiles
		glTexCoord2f(0.0f, 0.0f); glVertex3f((GLfloat)x, (GLfloat)y,  5.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f( (GLfloat)x+B_SIZE, (GLfloat)y,  5.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( (GLfloat)x+B_SIZE,  (GLfloat)y+B_SIZE,  5.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f((GLfloat)x,  (GLfloat)y+B_SIZE,  5.0f);	// Top Left Of The Texture and Quad
	glEnd();
	*/

	GLUquadric * sphere = gluNewQuadric();
	gluQuadricTexture(sphere, true);

	glPushMatrix();
	glTranslated(x + B_SIZE/2, y + B_SIZE/2,  5.0f);

	switch(direction){
		case LEFT:
			{
				glRotated(-90,0,0,1);
				break;
			}
		case DOWN:
			{
				//glRotated(0,0,0,1);
				break;
			}
		case RIGHT:
			{
				glRotated(90,0,0,1);
				break;
			}
		case UP:
			{
				glRotated(180,0,0,1);
				break;
			}
	};

	gluSphere(sphere, B_SIZE/3, 20, 20);
	glPopMatrix();
			
	gluDeleteQuadric(sphere);

	glColor3f(WHITE);
}

//////////////////////////////////////////////////////////////////////
//RACER CODE
///////////////////////////////////////////////////////////////////////
void Racer::ai()
{
	speed = 2*scale;
	static bool directions[4];
	directions[0] = false;
	directions[1] = false;
	directions[2] = false;
	directions[3] = false;

	/////////////////////////
	//check for intersection
	/////////////////////////
	if( (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0 )
	{
		if( 
			(direction == RIGHT || direction == LEFT) && 
			((map.tiles[posy-1][posx] != WALL && map.tiles[posy-1][posx] != SWALL) ||
			 (map.tiles[posy+1][posx] != WALL && map.tiles[posy+1][posx] != SWALL))   
			||
			(direction == UP || direction == DOWN) && 
			((map.tiles[posy][posx-1] != WALL && map.tiles[posy][posx-1] != SWALL) ||
			(map.tiles[posy][posx+1] != WALL && map.tiles[posy][posx+1] != SWALL))
		  )
		{   ////////////////////////////////
			//note the available directions
			////////////////////////////////
			if(map.tiles[posy][posx+1] != WALL && map.tiles[posy][posx+1] != SWALL)
				directions[RIGHT] = true;
			if(map.tiles[posy][posx-1] != WALL && map.tiles[posy][posx-1] != SWALL)
				directions[LEFT] = true;
			if(map.tiles[posy-1][posx] != WALL && map.tiles[posy-1][posx] != SWALL)
				directions[UP] = true;
			if(map.tiles[posy+1][posx] != WALL && map.tiles[posy+1][posx] != SWALL)
				directions[DOWN] = true;

			do//randomly pick from remaining directions
			{
				direction = (rand()%4);
			}while(!directions[direction]);
		}


		if(direction == RIGHT && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && map.tiles[posy][posx-1] != WALL && 
		   map.tiles[posy][posx-1] != SWALL)
			direction = LEFT;

		if(direction == LEFT && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && map.tiles[posy][posx+1] != WALL && 
		   map.tiles[posy][posx+1] != SWALL)
			direction = RIGHT;

		if(direction == UP && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && map.tiles[posy+1][posx] != WALL && 
		   map.tiles[posy+1][posx] != SWALL)
			direction = DOWN;

		if(direction == DOWN && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && map.tiles[posy-1][posx] != WALL && 
		   map.tiles[posy-1][posx] != SWALL)
			direction = UP;
				
	}



	////////////////
	//move Attacker
	///////////////
	switch(direction)
	{
	case RIGHT:
		{
			x+=speed;
			if(x > 504*scale-((float)W_WIDTH/2))//this code wraps pachead around the screen
			{
				if(state != SPEED)
					x = 2*scale - ((float)W_WIDTH/2);
				else
					x = 4*scale - ((float)W_WIDTH/2);
			}
		}break;
	case LEFT: 
		{
			x-=speed;
			if(x < 0-((float)W_WIDTH/2))
			{
				if(state != SPEED)
					x = 502*scale-((float)W_WIDTH/2);
				else
					x = 500*scale-((float)W_WIDTH/2);
			}

			
		}break;
	case DOWN:
		{
			y-=speed;
			if( y < (W_HEIGHT-(SIZEY*B_SIZE))-((float)W_HEIGHT/2))
				y = (GLfloat)W_HEIGHT-B_SIZE-((float)W_HEIGHT/2);
		}break;
	case UP: 
		{
			y+=speed;
			if( y > W_HEIGHT-B_SIZE-((float)W_HEIGHT/2))
				y = (GLfloat)W_HEIGHT-(SIZEY*B_SIZE)-((float)W_HEIGHT/2);

		}break;
	};
	
	//////////////////////
	//hold back if a wall
	//////////////////////
	if(direction == RIGHT && (map.tiles[posy][posx+1] == WALL || map.tiles[posy][posx+1] == SWALL))  
		x-=speed;
	if(direction == LEFT && (map.tiles[posy][posx-1] == WALL || map.tiles[posy][posx-1] == SWALL))
		x+=speed;
	if(direction == DOWN && (map.tiles[posy+1][posx] == WALL || map.tiles[posy+1][posx] == SWALL))
		y+=speed;
	if(direction == UP && (map.tiles[posy-1][posx] == WALL || map.tiles[posy-1][posx] == SWALL))
		y-=speed;	

  
		
	if(direction == DEAD)
	{
		if((int)x < map.x[initposy][initposx])
			x+=1;
		if((int)y < map.y[initposy][initposx])
			y+=1;
		if((int)x > map.x[initposy][initposx])
			x-=1;
		if((int)y > map.y[initposy][initposx])
			y-=1;

		if((int)x == map.x[initposy][initposx] && 
			(int)y == map.y[initposy][initposx] && 
			timeGetTime()-timer >= GHOSTTIME)
		{
			direction = RIGHT;
			posx = initposx;
			posy = initposy;
			
			x = map.x[initposy][initposx];
			y = map.y[initposy][initposx];
		}
	}
	/////////////////////////
	//update tile position
	/////////////////////////
	if( (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0 )
	{
		posx = (int)(x+((float)W_WIDTH/2))/B_SIZE;
		posy = 19-(int)(y+((float)W_HEIGHT/2))/B_SIZE;
	}
}

void Racer::drawenemy()
{
	if(direction == DEAD)
	{
		glBindTexture(GL_TEXTURE_2D, racersprites[DEAD][FRAME]);
	}
	else
	{
		glBindTexture(GL_TEXTURE_2D, racersprites[DOWN][FRAME]);
	}

	//glColor3f(ENEMYYELLOW);
	
	/*
	glBegin(GL_QUADS); //draw the tiles
		glTexCoord2f(0.0f, 0.0f); glVertex3f((GLfloat)x, (GLfloat)y,  5.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f( (GLfloat)x+B_SIZE, (GLfloat)y,  5.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( (GLfloat)x+B_SIZE,  (GLfloat)y+B_SIZE,  5.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f((GLfloat)x,  (GLfloat)y+B_SIZE,  5.0f);	// Top Left Of The Texture and Quad
	glEnd();
	*/

	GLUquadric * sphere = gluNewQuadric();
	gluQuadricTexture(sphere, true);

	glPushMatrix();
	glTranslated(x + B_SIZE/2, y + B_SIZE/2,  5.0f);

	switch(direction){
		case LEFT:
			{
				glRotated(-90,0,0,1);
				break;
			}
		case DOWN:
			{
				//glRotated(0,0,0,1);
				break;
			}
		case RIGHT:
			{
				glRotated(90,0,0,1);
				break;
			}
		case UP:
			{
				glRotated(180,0,0,1);
				break;
			}
	};

	gluSphere(sphere, B_SIZE/3, 20, 20);
	glPopMatrix();
			
	gluDeleteQuadric(sphere);

	glColor3f(WHITE);
}

